using System;
using System.Collections.Generic;
using System.Text;

namespace tKitchen
{
    public class MealPresenter
    {
        tKitchenMain domain;
        MealInput inputForm;
        Meal myMeal;
        MealPlan myMealPlan;


        public MealPresenter(tKitchenMain domainIn)
        {
            myMeal = new Meal();
            myMealPlan = new MealPlan();
            InitForm(domainIn);        
        }

        public MealPresenter(tKitchenMain domainIn, DateTime dateIn)
        {
            myMeal = new Meal();
            myMealPlan = new MealPlan();
            InitForm(domainIn);
            inputForm.Date = dateIn;
        }

        public MealPresenter(tKitchenMain domainIn, MealPlan mealPlanIn)
        {
            myMeal = mealPlanIn.Meal;
            myMealPlan = mealPlanIn;
            InitForm(domainIn);
            
            inputForm.MealName = myMealPlan.Meal.Name;
            inputForm.Date = myMealPlan.Date;
            inputForm.IncludedRecipe.DataSource = myMealPlan.Meal.Recipes;


        }

        private void InitForm(tKitchenMain domainIn)
        {
            domain = domainIn;
            domain.RecipeAdded += new EventHandler(domain_RecipeAdded);
            domain.RecipeUpdated += new EventHandler(domain_RecipeUpdated);

            inputForm = new MealInput();
            inputForm.NewRecipeButtonClick += new EventHandler(inputForm_NewRecipeButtonClick);
            inputForm.AddButtonClick += new EventHandler(inputForm_AddButtonClick);
            inputForm.RemoveButtonClick += new EventHandler(inputForm_RemoveButtonClick);
            inputForm.SaveButtonClick += new EventHandler(inputForm_SaveButtonClick);

            PopulateRecipeList();
            inputForm.IncludedRecipe.DataSource = new List<Meal_Recipe>();
            inputForm.IncludedRecipe.DisplayMember = "Recipe";
        }




        #region Event Handlers
        void inputForm_SaveButtonClick(object sender, EventArgs e)
        {
            myMeal.Name = inputForm.MealName;
            myMeal.Recipes = (List<Meal_Recipe>)inputForm.IncludedRecipe.DataSource;
            //myMeal.Comments
            //myMeal.Rating

            myMealPlan.Meal = myMeal;
            myMealPlan.Date = inputForm.Date;
            //myMealPlan.Notes

            domain.SaveObject(myMealPlan);

        }
        void inputForm_AddButtonClick(object sender, EventArgs e)
        {

            if (inputForm.AllRecipe.SelectedIndex > -1)
            {
                Meal_Recipe temp = (Meal_Recipe)inputForm.AllRecipe.SelectedItem;
                List<Meal_Recipe> includedList = (List<Meal_Recipe>)inputForm.IncludedRecipe.DataSource;
                includedList.Add(temp);
                PopulateIncludedList(includedList);

                //if there is nothing in the name box yet
                //fill it in with the name of this recipe
                if (inputForm.MealName.Equals(""))
                {
                    inputForm.MealName = temp.Recipe.Name;
                }

            }
            else
            {
                //Console.Out.WriteLine("Nothing Selected to Add!");
            }
        }
        void inputForm_RemoveButtonClick(object sender, EventArgs e)
        {
            if (inputForm.IncludedRecipe.SelectedIndex > -1)
            {
                Meal_Recipe temp = (Meal_Recipe)inputForm.IncludedRecipe.SelectedItem;
                List<Meal_Recipe> includedList = (List<Meal_Recipe>)inputForm.IncludedRecipe.DataSource;
                includedList.Remove(temp);
                PopulateIncludedList(includedList);
            }
            else
            {
                //Console.Out.WriteLine("Nothing Selected to Remove!");
            }
        }

        void inputForm_NewRecipeButtonClick(object sender, EventArgs e)
        {
            RecipePresenter presenter = new RecipePresenter(domain);
            presenter.ShowForm();
        }

        void domain_RecipeAdded(object sender, EventArgs e)
        {
            PopulateRecipeList();
        }
        void domain_RecipeUpdated(object sender, EventArgs e)
        {
            PopulateRecipeList();
        }

        #endregion



        private void PopulateRecipeList()
        {
            List<Recipe> recipeList = domain.GetRecipeList();
            List<Meal_Recipe> tempList = new List<Meal_Recipe>();
            foreach (Recipe rec in recipeList)
            {
                Meal_Recipe temp = new Meal_Recipe();
                temp.Recipe = rec.Copy();

                tempList.Add(temp);
            }


            inputForm.AllRecipe.DataSource = tempList;
            inputForm.AllRecipe.DisplayMember = "Recipe";

        }
        private void PopulateIncludedList(List<Meal_Recipe> listIn)
        {
            inputForm.IncludedRecipe.DataSource = null;
            inputForm.IncludedRecipe.DataSource = listIn;
            inputForm.IncludedRecipe.DisplayMember = "Recipe";
        }

        internal void ShowForm()
        {
            inputForm.Show();
        }
    }
}
